# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import argparse
import cv2, os

from taa_core.config import cfg
from demo.predictor import COCODemo

import time

name = 'atss'
def main():
    parser = argparse.ArgumentParser(description="PyTorch Object Detection Webcam Demo")
    parser.add_argument(
        "--config-file",
        default=f"configs/{name}/{name}_R_50_FPN_1x.yaml",
        metavar="FILE",
        help="path to config file",
    )
    parser.add_argument(
        "--weights",
        default=f"training_dir/{name}50.pth",
        metavar="FILE",
        help="path to the trained model",
    )
    parser.add_argument(
        "--images-dir",
        default="e:/datasets/coco/val2017",
        metavar="DIR",
        help="path to demo images directory",
    )
    parser.add_argument(
        "--min-image-size",
        type=int,
        default=800,
        help="Smallest size of the image to feed to the model. "
            "Model was trained with 800, which gives best results",
    )
    parser.add_argument(
        "opts",
        help="Modify model config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

    args = parser.parse_args()

    # load config from file and command-line arguments
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    cfg.MODEL.WEIGHT = args.weights

    cfg.freeze()

    # The following per-class thresholds are computed by maximizing
    # per-class f-measure in their precision-recall curve.
    # Please see compute_thresholds_for_classes() in coco_eval.py for details.
    # fcos
    # thresholds_for_classes = [
    #     0.4923645853996277, 0.4928510785102844, 0.5040897727012634,
    #     0.4912887513637543, 0.5016880631446838, 0.5278812646865845,
    #     0.5351834893226624, 0.5003424882888794, 0.4955945909023285,
    #     0.43564629554748535, 0.6089804172515869, 0.666087806224823,
    #     0.5932040214538574, 0.48406165838241577, 0.4062422513961792,
    #     0.5571075081825256, 0.5671307444572449, 0.5268378257751465,
    #     0.5112953186035156, 0.4647842049598694, 0.5324517488479614,
    #     0.5795850157737732, 0.5152440071105957, 0.5280804634094238,
    #     0.4791383445262909, 0.5261335372924805, 0.4906163215637207,
    #     0.523737907409668, 0.47027698159217834, 0.5103300213813782,
    #     0.4645252823829651, 0.5384289026260376, 0.47796186804771423,
    #     0.4403403103351593, 0.5101461410522461, 0.5535093545913696,
    #     0.48472103476524353, 0.5006796717643738, 0.5485560894012451,
    #     0.4863888621330261, 0.5061569809913635, 0.5235867500305176,
    #     0.4745445251464844, 0.4652363359928131, 0.4162440598011017,
    #     0.5252017974853516, 0.42710989713668823, 0.4550687372684479,
    #     0.4943239390850067, 0.4810051918029785, 0.47629663348197937,
    #     0.46629616618156433, 0.4662836790084839, 0.4854755401611328,
    #     0.4156557023525238, 0.4763634502887726, 0.4724511504173279,
    #     0.4915047585964203, 0.5006274580955505, 0.5124194622039795,
    #     0.47004589438438416, 0.5374764204025269, 0.5876904129981995,
    #     0.49395060539245605, 0.5102297067642212, 0.46571290493011475,
    #     0.5164387822151184, 0.540651798248291, 0.5323763489723206,
    #     0.5048757195472717, 0.5302401781082153, 0.48333442211151123,
    #     0.5109739303588867, 0.4077408015727997, 0.5764586925506592,
    #     0.5109297037124634, 0.4685552418231964, 0.5148998498916626,
    #     0.4224434792995453, 0.4998510777950287
    # ]

    # mla
    # thresholds_for_classes = [0.7245147228240967, 0.7315584421157837, 0.7425212264060974, 0.7207880616188049, 
    #   0.7748095393180847, 0.7607349157333374, 0.8298381567001343, 0.7393877506256104, 0.7294775247573853, 0.7037956118583679, 
    #   0.7669826745986938, 0.7914382815361023, 0.8046911954879761, 0.7407982349395752, 0.7000380158424377, 0.7844155430793762, 
    #   0.8055731058120728, 0.782930314540863, 0.7680105566978455, 0.7562739253044128, 0.7638345956802368, 0.8138859272003174, 
    #   0.7948622703552246, 0.7764908075332642, 0.688230574131012, 0.7343406081199646, 0.675341784954071, 0.714955747127533, 
    #   0.7212905883789062, 0.778978705406189, 0.690825343132019, 0.7389686703681946, 0.7612459659576416, 0.7341263294219971, 
    #   0.7528024315834045, 0.7611910104751587, 0.7624908089637756, 0.7302805781364441, 0.7314102053642273, 0.7231070399284363, 
    #   0.7126315236091614, 0.7468417882919312, 0.7376147508621216, 0.6899188160896301, 0.7143146991729736, 0.743713915348053, 
    #   0.7238736152648926, 0.7214438915252686, 0.7619379162788391, 0.7109085321426392, 0.7098374366760254, 0.7182548642158508, 
    #   0.7513015270233154, 0.750678539276123, 0.6954823136329651, 0.7257372736930847, 0.7270689010620117, 0.7562626004219055, 
    #   0.7343472838401794, 0.7854009866714478, 0.7519499659538269, 0.8040674924850464, 0.76894211769104, 0.7662751078605652, 
    #   0.777724027633667, 0.7034693360328674, 0.8098947405815125, 0.7454416751861572, 0.7938978672027588, 0.7522071599960327, 
    #   0.7777824997901917, 0.7579033970832825, 0.7627827525138855, 0.665738046169281, 0.7973730564117432, 0.753433108329773, 
    #   0.762147843837738, 0.773094892501831, 0.6798238754272461, 0.7353936433792114
    # ]

    # atss
    thresholds_for_classes = [
        0.4923645853996277, 0.4928510785102844, 0.5040897727012634,
        0.4912887513637543, 0.5016880631446838, 0.5278812646865845,
        0.5351834893226624, 0.5003424882888794, 0.4955945909023285,
        0.43564629554748535, 0.6089804172515869, 0.666087806224823,
        0.5932040214538574, 0.48406165838241577, 0.4062422513961792,
        0.5571075081825256, 0.5671307444572449, 0.5268378257751465,
        0.5112953186035156, 0.4647842049598694, 0.5324517488479614,
        0.5795850157737732, 0.5152440071105957, 0.5280804634094238,
        0.4791383445262909, 0.5261335372924805, 0.4906163215637207,
        0.523737907409668, 0.47027698159217834, 0.5103300213813782,
        0.4645252823829651, 0.5384289026260376, 0.47796186804771423,
        0.4403403103351593, 0.5101461410522461, 0.5535093545913696,
        0.48472103476524353, 0.5006796717643738, 0.5485560894012451,
        0.4863888621330261, 0.5061569809913635, 0.5235867500305176,
        0.4745445251464844, 0.4652363359928131, 0.4162440598011017,
        0.5252017974853516, 0.42710989713668823, 0.4550687372684479,
        0.4943239390850067, 0.4810051918029785, 0.47629663348197937,
        0.46629616618156433, 0.4662836790084839, 0.4854755401611328,
        0.4156557023525238, 0.4763634502887726, 0.4724511504173279,
        0.4915047585964203, 0.5006274580955505, 0.5124194622039795,
        0.47004589438438416, 0.5374764204025269, 0.5876904129981995,
        0.49395060539245605, 0.5102297067642212, 0.46571290493011475,
        0.5164387822151184, 0.540651798248291, 0.5323763489723206,
        0.5048757195472717, 0.5302401781082153, 0.48333442211151123,
        0.5109739303588867, 0.4077408015727997, 0.5764586925506592,
        0.5109297037124634, 0.4685552418231964, 0.5148998498916626,
        0.4224434792995453, 0.4998510777950287
    ]
    

    demo_im_names = os.listdir(args.images_dir)

    # prepare object that handles inference plus adds predictions on top of image
    coco_demo = COCODemo(
        cfg,
        confidence_thresholds_for_classes=thresholds_for_classes,
        min_image_size=args.min_image_size
    )

    for im_name in demo_im_names:
        img = cv2.imread(os.path.join(args.images_dir, im_name))
        if img is None:
            continue
        start_time = time.time()
        composite, sname = coco_demo.run_on_opencv_image(img)
        print("{}\tinference time: {:.2f}s".format(im_name, time.time() - start_time))
        cv2.imwrite(F"{dir_name}/{im_name}_{sname}.jpg", composite)
    print("Press any keys to exit ...")
    cv2.waitKey()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    dir_name = f"preds/{name}_al"
    if not os.path.exists(dir_name):
        os.mkdir(dir_name)
    main()

